#include<stdio.h>
#include<sys/stat.h>
#include<string.h>
#include<wait.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
  int fd[2];
  if(pipe(fd)<0)
  {
    perror("pipi");
    return 1;
  }
  pid_t id=fork();
  if(id==0)
  {
    //子进程
    close(fd[0]);//关闭读端
    const char* msg="hello world";
    int count=10;
    while(count--)
    {
      write(fd[1],msg,strlen(msg));
      sleep(1);
    }
    close(fd[1]);
    exit(0);
  }
  close(fd[1]);
  char buffer[20];
  while(1)
  {
    ssize_t reallysize=read(fd[0],buffer,sizeof(buffer));
    if(reallysize>0)
    {
      buffer[reallysize]='\0';
      printf("father receive:%s\n",buffer);
    }
    else if(reallysize==0)
    {
      //读取完
      printf("read over\n");
      break;
    }
    else 
    {
      printf("read error\n");
      break;
    }
  }
  waitpid(id,NULL,0);
  return 0;
}
